package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.util.viewmodel.ApiResultV2;
import study.saas.common.util.viewmodel.PageModel;
import study.saas.order.entity.Employee;
import study.saas.order.entity.Porder;
import study.saas.order.repo.EmployeeRepo;
import study.saas.order.repo.PorderRepo;
import study.saas.order.service.model.EmployeeCreateDTO;
import study.saas.order.service.model.PorderAddDTO;
import study.saas.order.service.model.PorderFilterDTO;
import study.saas.order.service.model.PorderModifyDTO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wwy
 */
@Service
public class PorderService {

    @Resource
    private PorderRepo porderRepo;

    public ApiResultV2<Porder> findPorder(Integer porderId){
        Porder porder =porderRepo.findByPorderId(porderId);
        if(porder !=null &&porder.isDelete()){
            porder =null;
        }
        return ApiResultV2.ok(porder);
    }

    public ApiResultV2 addPorder(PorderAddDTO add) {
        Porder porder =porderRepo.findByPorderId(add.getPorderId());
        if (porder != null && !porder.isDelete()) {
            return ApiResultV2.error("id重复");
        }
        Porder newPorder;
        if (porder == null) {
            newPorder = new Porder();
        }
        else {
            newPorder = porder;
            newPorder.setIsDeleted(0);
        }
        CopyClassUtil.copyClass(add, newPorder);
        porderRepo.save(newPorder);
        return ApiResultV2.ok();
    }

    public ApiResultV2 deletePorder(Integer porderId){
        Porder porder =porderRepo.findByPorderId(porderId);
        if(porder==null ||porder.isDelete()){
            return ApiResultV2.ok();
        }
        porder.setIsDeleted(1);
        porderRepo.save(porder);
        return ApiResultV2.ok();
    }

    public ApiResultV2 modifyPorder(PorderModifyDTO modify){
        Porder porder =porderRepo.findByPorderId(modify.getPorderId());
        if(porder ==null || porder.isDelete()){
            return ApiResultV2.error("订单不存在！");
        }
        CopyClassUtil.copyClass(modify,porder);
        porderRepo.save(porder);
        return ApiResultV2.ok();
    }

    public ApiResultV2 <PageModel<Porder>> listPorder(PorderFilterDTO filter){
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select p from Porder p where p.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getPorderCommodityNameEq())){
            sql.append(" and p.porderCommodityName = :getPorderCommodityNameEq");
            param.put("getPorderCommodityNameEq", filter.getPorderCommodityNameEq());
        }
        if (!StringUtil.isEmpty(filter.getPorderCommodityNameLike())){
            sql.append(" and p.porderCommodityName like :getPorderCommodityNameLike");
            param.put("getPorderCommodityNameLike", "%" + filter.getPorderCommodityNameLike() + "%");
        }

        sql.append(" order by p.createTime desc,uuid desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Porder> pageModel = porderRepo.queryPage(sql.toString(), param, Porder.class, pageable);
        return ApiResultV2.ok(pageModel);
    }


}
